package org.dllwh.utils.apache.poi.word;

import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.math.BigInteger;

import static org.dllwh.utils.apache.poi.word.conf.WordConstant.*;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName WordCatalogueHelper
 * @Description Word目录处理工具类
 * @Date 2025-10-24 13:54
 * @版本 V 1.0.1
 * @since JDK 17
 */
public final class WordCatalogue {
    /**
     * 生成目录页
     */
    public static void buildTocPage(XWPFDocument document) {
        // 增加分页符
        document.createParagraph().createRun().addBreak(BreakType.PAGE);
    }

    /**
     * 创建章节标题
     *
     * @param document 文档对象
     * @param text     标题内容
     * @param level    标题级别
     */
    public static void createChapterTitle(XWPFDocument document, String text, int level) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        paragraph.setStyle("Heading" + level);

        // 设置标题样式
        if (level == 1) {
            run.setBold(true);
            run.setFontSize(FONT_SIZE_2);
            run.setFontFamily(FONT_NAME_HEI);
        } else if (level == 2) {
            run.setBold(true);
            run.setFontSize(FONT_SIZE_3);
            run.setFontFamily(FONT_NAME_KAI);
        } else if (level == 3) {
            run.setFontSize(FONT_SIZE_4);
            run.setFontFamily(FONT_NAME_FANG);
        } else if (level == 4) {
            run.setFontSize(FONT_SIZE_4);
            run.setFontFamily(FONT_NAME_FANG);
        }

        // 设置大纲级别
        paragraph.getCTP().addNewPPr().addNewOutlineLvl().setVal(BigInteger.valueOf(level - 1));
    }
}
